package com.xinz.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Random;

/**
 * 时间辅助类
 **/
public class DateUtil {

	/**
	 * 随机对象
	 **/
	public static final Random RND = new Random();

	public static int now() {
		return (int) (System.currentTimeMillis() / 1000);
	}
	
	public static int getNowDay(){
		//获得每个星期的天数
		int nowDay = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
		if(nowDay==1)
			nowDay = 8;
		return nowDay-1;
	}
	
    /**
     * 计算两个时间之间相隔的天数
     * 
     * @param startTime
     *            开始时间，以秒为单位
     * @param endTime
     *            结束时间，以秒为单位
     * @return
     */
    public static int dayDelta(int startTime, int endTime) {

        if (startTime > endTime) {
            return 0;
        }

        GregorianCalendar cal1 = new GregorianCalendar();
        GregorianCalendar cal2 = new GregorianCalendar();

        cal1.setTime(new Date(startTime * 1000l));
        cal2.setTime(new Date(endTime * 1000l));

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);

        int delta = 0;
        if (year1 < year2) {
            if (cal1.isLeapYear(year1)) {
                delta = 366 - cal1.get(Calendar.DAY_OF_YEAR);
            } else {
                delta = 365 - cal1.get(Calendar.DAY_OF_YEAR);
            }

            delta += cal2.get(Calendar.DAY_OF_YEAR);
        } else {
            delta = cal2.get(Calendar.DAY_OF_YEAR) - cal1.get(Calendar.DAY_OF_YEAR);
        }

        return delta;
    }
    
    public static String formatTime(int time, String pattern) {
        SimpleDateFormat myFormatter = new SimpleDateFormat(pattern);
        try {
            return myFormatter.format(new Date(time * 1000L));
        } catch (Exception e) {
            return "";
        }
    }
    
    public static boolean isSameDate(String date1,String date2) {
	    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	    Date d1 = null;
	    Date d2 = null;
	    try {
		    d1 = format.parse(date1);
		    d2 = format.parse(date2);
	    } catch(Exception e) {
	    	e.printStackTrace();
	    }
	    Calendar cal1 = Calendar.getInstance();
	    Calendar cal2 = Calendar.getInstance();
	    cal1.setTime(d1);
	    cal2.setTime(d2);
	    int subYear = cal1.get(Calendar.YEAR)-cal2.get(Calendar.YEAR);
	    //subYear==0,说明是同一年
	    if(subYear == 0){
		    if(cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
		    return true;
	    }
	    //例子:cal1是"2005-1-1"，cal2是"2004-12-25"
	    //java对"2004-12-25"处理成第52周
	    // "2004-12-26"它处理成了第1周，和"2005-1-1"相同了
	    //说明:java的一月用"0"标识，那么12月用"11"
	    else if(subYear==1 && cal2.get(Calendar.MONTH)==11){
		    if(cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
		    return true;
	    }
	    //例子:cal1是"2004-12-31"，cal2是"2005-1-1"
	    else if(subYear==-1 && cal1.get(Calendar.MONTH)==11){
		    if(cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
		    return true;
	    }
	    return false;
    }
    
    /***
	 * 判断星期几是否有签到
	 * @param signValue 签到的数
	 * @param weekValue 星期几(0-星期天，1-星期六...6-星期一)
	 * @return
	 */
	public static boolean isSign(int signValue,int weekValue){
		boolean signFlag = false;
			if((signValue & (1 << weekValue)) == (1 << weekValue)){
				signFlag = true;
			}
		return signFlag;
	}
    
    public static void main(String args[]) throws ParseException {
    	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    	Date d1 = format.parse("2012-07-31");
    	Date d2 = format.parse("2012-08-01");
    	System.out.println(d1.getTime());
    	System.out.println(d2.getTime());
    	int dd1 = (int)(d1.getTime()/1000);
    	int dd2 = (int)(d2.getTime()/1000);
    	int d = dayDelta(dd1, dd2);
    	System.out.println(d);
    }

}
